<?php

/**
 * This is the model class for table "hd_user".
 *
 * The followings are the available columns in table 'hd_user':
 * @property string $id
 * @property string $username
 * @property string $password
 * @property integer $user_type_id
 * @property integer $is_deleted
 *
 * The followings are the available model relations:
 * @property Job[] $jobs
 * @property UserType $userType
 */
class User extends CActiveRecord
{
	public $password_repeat;
	public $user_type_name;
	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'hd_user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, password', 'required'),
			array('user_type_id', 'numerical', 'integerOnly'=>true),
			array('username, full_name', 'length', 'max'=>50),
			array('password', 'length', 'max'=>256),
			array('username','unique'),
			array('password','compare'),
			array('password_repeat,user_type_name','safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, password, user_type_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'jobs' => array(self::HAS_MANY, 'Job', 'user_id'),
			'userType' => array(self::BELONGS_TO, 'UserType', 'user_type_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'Tên đăng nhập',
			'password' => 'Mật khẩu',
			'password_repeat' => 'Xác nhận lại mật khẩu',
			'user_type_id' => 'Loại người dùng',
			'full_name' => 'Họ và tên',
			'user_type_name' => 'Loại người dùng',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		$criteria=new CDbCriteria;
		$criteria->order = 'id';
		$criteria->condition = 'user_type_id !=:user_type_id and is_deleted =:is_deleted';		// don't display users belong to admin group
		$criteria->params = array(':user_type_id'=>Common::getAdminCode(), ':is_deleted'=>Common::getUnDeletedCode());
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        //get User
	public function getUser($id)
	{
		return User::model()->findAllByAttributes(array('id' => $id));
	}

	public function encrypt($value)
	{
		return md5($value);
	}
	public function encryptPassword(){
		$this->password = $this->encrypt($this->password);
	}
	
	public function deleteEx(){
		$this->setAttribute('is_deleted', 1);
		$this->update(array('is_deleted'));
	}
}